package com.zxy.dao.v1;

import com.zxy.entity.v1.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description: TODO
 * @Author: zhuxianyu
 * @Date: 2021/12/17 21:15
 * @Version: 1.0
 */
@Repository
public class PersonDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Person> selectList(int n) {
        String sql = "select * from person limit " + n;
        List<Person> personList = jdbcTemplate.query(sql, new RowMapper<Person>() {
            @Override
            public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
                //从结果集里把数据得到
                String uuid = rs.getString("uuid");
                String distinct_id = rs.getString("distinct_id");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String nick_name = rs.getString("nick_name");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String age = rs.getString("age");
                long signup_time = rs.getLong("signup_time");
                String type = rs.getString("type");

                //把数据封装到对象里
                Person person = new Person();
                person.setUuid(uuid);
                person.setDistinct_id(distinct_id);
                person.setMobile(mobile);
                person.setEmail(email);
                person.setNick_name(nick_name);
                person.setName(name);
                person.setGender(gender);
                person.setAge(age);
                person.setSignup_time(signup_time);
                person.setType(type);
                return person;
            }
        });
        return personList;
    }
}
